package com.tqz.juc.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2022/2/6 17:52
 */
public class AtomicIntegerFieldUpdaterTest {
    
    /**
     * 修改的属性不能用 `private`修饰，必须用volatile保证可见性，且必须是integer
     */
    static AtomicIntegerFieldUpdater<User> updater = AtomicIntegerFieldUpdater.newUpdater(User.class, "id");
    
    public static void main(String[] args) {
        User user = new User(1, "zs");
        System.out.println(updater.getAndIncrement(user));
        System.out.println(updater.get(user));
    }
    
    static class User {
        
        volatile int id;
        
        String name;
        
        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
}
